import { patOrdersPage, patOrdersUpdate } from "@/api/MedicalExamination";
import { chargeApi } from "@/api/MedicalExamination/outpatientExpenses/charge";
import { bookkeepApi } from "@/api/statisticalReport/financialTransactions";
import { getSimpleDeptList } from "@/api/system/dept";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { ApiSelect } from "@/components/Form";
import { formatDate } from "@/utils/util";
import { Descriptions, message, Select } from "antd";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
const { Item } = Descriptions;
let handleSubmitRegistration: Function;
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const [page, setPage] = useState("1");
	const [patOrders, setPatOrders] = useState<any>({});
	const [orderId, setOrderId] = useState<any>({});
	const [departmentName, setDepartmentName] = useState<any>("");
	const [debitAccount, setDebitAccount] = useState("");
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, async data => {
		setOrderId(data.res);
		handleSubmitRegistration = data.handleSubmitRegistration;
	});
	const options = [
		{
			label: t("MedicalExamination.outpatientExpenses.charge.cash"),
			value: "1"
		},
		{
			label: t("MedicalExamination.outpatientExpenses.charge.socialSecurity"),
			value: "2"
		},
		{
			label: t("MedicalExamination.outpatientExpenses.charge.bankCard"),
			value: "3"
		},
		{
			label: t("MedicalExamination.outpatientExpenses.charge.others"),
			value: "4"
		}
	];
	const handleChange = (value: string) => {
		setPage(value);
	};
	const handleSubmit = async () => {
		const res = await patOrdersUpdate({
			orderId,
			status: 1,
			itemslist: [
				{
					pkId: patOrders.pkId,
					status: 1
				}
			]
		});
		if (!res) return message.error(t("MedicalExamination.orderDetails.paymentFailed"));
		message.success(t("MedicalExamination.orderDetails.paymentSuccessful"));
		await bookkeepApi
			.bookkeepCreate({
				debitAccount,
				creditAccount: patOrders.patCardno,
				creditAmount: patOrders.totalPrice,
				debitAmount: patOrders.totalPrice
			})
			.then(res => {
				console.log(res);
			});
		closeDrawer();
		handleSubmitRegistration();
	};
	const apiChange = (value: any) => {
		setDebitAccount(value);
	};
	const patOrdersFn = async () => {
		const res = await patOrdersPage({
			orderId
		});
		setPatOrders(res.list[0]);
		const department = await getSimpleDeptList();
		department.forEach((item: any) => {
			if (item.id == res.list[0].deptId) {
				setDepartmentName(item.name);
			}
		});
	};
	useEffect(() => {
		if (!orderId) return;
		patOrdersFn();
	}, [orderId]);
	return (
		<BasicDrawer showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<div>
				{patOrders && patOrders.patients ? (
					<Descriptions column={1} layout="horizontal">
						<Item label={t("MedicalExamination.orderDetails.orderNumber")}>{patOrders.orderId}</Item>
						<Item label={t("MedicalExamination.orderDetails.cardNumber")}>{patOrders.patCardno}</Item>
						<Item label={t("MedicalExamination.orderDetails.name")}>{patOrders.patients.fullName}</Item>
						<Item label={t("MedicalExamination.orderDetails.age")}>{patOrders.patients.age}</Item>
						<Item label={t("MedicalExamination.orderDetails.gender")}>{patOrders.patients.gender}</Item>
						<Item label={t("MedicalExamination.orderDetails.ethnicity")}>{patOrders.patients.ethnicity}</Item>
						<Item label={t("MedicalExamination.orderDetails.dateOfBirth")}>{patOrders.patients.birthday}</Item>
						<Item label={t("MedicalExamination.orderDetails.department")}>{departmentName}</Item>
						<Item label={t("MedicalExamination.orderDetails.paymentAmount")}>{patOrders.totalPrice}</Item>
						<Item label={t("MedicalExamination.outpatientExpenses.charge.receivingAccount")} span={24}>
							<ApiSelect
								api={chargeApi.accountsList}
								labelField="accountNo"
								valueField="accountNo"
								onChange={apiChange}
							></ApiSelect>
						</Item>
						<Item label={t("MedicalExamination.orderDetails.paymentType")}>
							<Select defaultValue={page} style={{ width: 120 }} onChange={handleChange} options={options} />
						</Item>
						<Item label={t("MedicalExamination.orderDetails.creationTime")}>
							{formatDate(new Date(patOrders.patients.createdTime))}
						</Item>
					</Descriptions>
				) : (
					""
				)}
			</div>
		</BasicDrawer>
	);
};
export default index;
